package exsun.jt808.data.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.internal.bind.ObjectTypeAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class ObjectTransUtil {

//    public static Gson gson = new GsonBuilder().disableHtmlEscaping()
//            .setDateFormat("yyyy-MM-dd HH:mm:ss").create();
    public static Gson gson = getGson();
    static private final Logger logger = LoggerFactory.getLogger(ObjectTransUtil.class);
    private ObjectTransUtil(){}


    /**
     * 返回数据列对应的字段数据
     * @param data gps数据实体
     * @return
     */
    public static Map<String,byte[]> ObjectToMapBytes(Object data)
    {
        Map<String, byte[]> result = new LinkedHashMap<>();
        try {

            Field[] fields = data.getClass().getFields();
            for (Field property : fields ) {
                String name = property.getName();
                Object filedObject = property.get(data);
                byte[] value = gson.toJson(filedObject).getBytes();
                result.put(name,value);
            }
        }
        catch (Exception ex)
        {
            logger.error(ex.getMessage());
        }
        return result;
    }


    public static  <T> T MapBytesToObject(Map<String,byte[]> mapValue, Class<T> tClass) throws InstantiationException ,
            IllegalAccessException {
        T instance = tClass.newInstance();
        Field[] fileds = instance.getClass().getFields();
        for (Field property :
                fileds
             ) {
            String name = property.getName();
            byte[] value = mapValue.get(name);
            if (value != null)
            {
                String json = new String(value);
                if(json.equals("null")){
                    property.set(instance,null);
                    continue;
                }
                property.set(instance,gson.fromJson(json,property.getType()));
            }
        }
        return instance;
    }

    public static Gson getGson() {
        Gson gson = new GsonBuilder().create();
        try {
            Field factories = Gson.class.getDeclaredField("factories");
            factories.setAccessible(true);
            Object o = factories.get(gson);
            Class<?>[] declaredClasses = Collections.class.getDeclaredClasses();
            for (Class c : declaredClasses) {
                if ("java.util.Collections$UnmodifiableList".equals(c.getName())) {
                    Field listField = c.getDeclaredField("list");
                    listField.setAccessible(true);
                    List<TypeAdapterFactory> list = (List<TypeAdapterFactory>) listField.get(o);
                    int i = list.indexOf(ObjectTypeAdapter.FACTORY);
                    list.set(i, MapTypeAdapter.FACTORY);
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return gson;
    }

}
